package com.macro.mall.service.impl;

import com.macro.mall.mapper.BmsMuseumPicMapper;
import com.macro.mall.model.BmsMuseumPic;
import com.macro.mall.model.BmsMuseumPicExample;
import com.macro.mall.service.BmsMuseumPicService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BmsMuseumPicServiceImpl implements BmsMuseumPicService {

    @Autowired
    private BmsMuseumPicMapper bmsMuseumPicMapper;

    @Override
    public void create(Long museumId, String coverPic, List<String> picUrlList) {

        BmsMuseumPic itemObject;

        if (StringUtils.isNotBlank(coverPic)) {
            itemObject = new BmsMuseumPic();
            itemObject.setPic(coverPic);
            itemObject.setMuseumId(museumId);
            itemObject.setIsCover((byte) 1);
            bmsMuseumPicMapper.insert(itemObject);
        }

        if (CollectionUtils.isNotEmpty(picUrlList)) {
            for (String pic : picUrlList) {
                itemObject = new BmsMuseumPic();
                itemObject.setPic(pic);
                itemObject.setMuseumId(museumId);
                itemObject.setIsCover((byte) 0);
                bmsMuseumPicMapper.insert(itemObject);
            }
        }
    }

    @Override
    public void deleteByMuseumId(Long museumId) {
        BmsMuseumPicExample example = new BmsMuseumPicExample();
        BmsMuseumPicExample.Criteria criteria = example.createCriteria();
        criteria.andMuseumIdEqualTo(museumId);
        bmsMuseumPicMapper.deleteByExample(example);
    }

    @Override
    public String findCoverPicByMuseumId(Long museumId) {
        BmsMuseumPicExample example = new BmsMuseumPicExample();
        BmsMuseumPicExample.Criteria criteria = example.createCriteria();
        criteria.andMuseumIdEqualTo(museumId);
        criteria.andIsCoverEqualTo((byte) 1);
        List<BmsMuseumPic> bmsMuseumPics = bmsMuseumPicMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(bmsMuseumPics)) {
            return bmsMuseumPics.get(0).getPic();
        }
        return null;
    }

    @Override
    public List<String> findPicListByMuseumId(Long museumId) {
        BmsMuseumPicExample example = new BmsMuseumPicExample();
        BmsMuseumPicExample.Criteria criteria = example.createCriteria();
        criteria.andMuseumIdEqualTo(museumId);
        criteria.andIsCoverEqualTo((byte) 0);
        List<BmsMuseumPic> bmsMuseumPics = bmsMuseumPicMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(bmsMuseumPics)) {
            return bmsMuseumPics.stream().map(BmsMuseumPic::getPic).collect(Collectors.toList());
        }
        return null;
    }

}
